package com.hdkj.eduservice.controller;

import com.hdkj.commonutils.JwtUtils;
import com.hdkj.commonutils.R;
import com.hdkj.commonutils.ordervo.UcenterMemberOrder;
import com.hdkj.eduservice.client.UcenterClient;
import com.hdkj.eduservice.entity.ArtSubject;
import com.hdkj.eduservice.entity.EduArticle;
import com.hdkj.eduservice.service.ArtSubjectService;
import com.hdkj.eduservice.service.ArticleService;
import com.hdkj.handler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/eduservice/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private ArtSubjectService artSubjectService;


    //根据文章的分类查询

    @RequestMapping("getarticleByasubid/{asbid}")
    public R getarticleByasubid(@PathVariable String asbid) {
        List twoIds = new ArrayList();
        List avatars = new ArrayList();
        List nicknames = new ArrayList();
        List<EduArticle> articleList = null;
        HashMap<String, List> map = null;
        try {
            articleList = articleService.list(new QueryWrapper<EduArticle>().eq("two_id", asbid));
            map = new HashMap<>();
            for (EduArticle article : articleList) {
                twoIds.add(article.getTwoId());
            }
            List<ArtSubject> ArtSubject = artSubjectService.getByIds(twoIds);
            List<UcenterMemberOrder> userInfoOrders = ucenterClient.getUserInfoOrders(articleList.stream().map(EduArticle::getUserId).collect(Collectors.toList()));
            for (EduArticle eduArticle : articleList) {
                for (UcenterMemberOrder userInfoOrder : userInfoOrders) {
                    if (eduArticle.getUserId() == userInfoOrder.getId()) {
                        avatars.add(userInfoOrder.getAvatar());
                        nicknames.add(userInfoOrder.getNickname());
                    }
                }
            }
            map.put("avatars", avatars);
            map.put("nicknames", nicknames);
            map.put("ArtSubject", ArtSubject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("total", articleList.size()).data("rows", articleList).data("map", map);
    }


    //查询所有的文章带分页
    @RequestMapping("getarticlelist/{page}/{limit}")
    public R getallarticle(@ApiParam(name = "page", value = "当前页码", required = true)
                           @PathVariable long page,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                           @PathVariable long limit, HttpServletRequest request
    ) {
        long total = 0;
        List<EduArticle> articleList = null;
        HashMap<String, List> map=new HashMap<>();
        try {
            Page<EduArticle> pageParam = new Page<>(page, limit);
            articleList = articleService.page(pageParam).getRecords();
            if (articleList.size()==0||articleList==null){
                throw  new GuiliException(2001,"null");
            }
            List Avatars = new ArrayList();
            List<String> nicknames = new ArrayList();
            List twoIds = new ArrayList();
            map = new HashMap<>();
            for (EduArticle article : articleList) {
                twoIds.add(article.getTwoId());
            }
            List<ArtSubject> ArtSubject = artSubjectService.getByIds(twoIds);
            List<UcenterMemberOrder> userInfoOrders = ucenterClient.getUserInfoOrders(articleList.stream().map(EduArticle::getUserId).collect(Collectors.toList()));
            for (EduArticle eduArticle : articleList) {
                for (UcenterMemberOrder userInfoOrder : userInfoOrders) {
                    if (eduArticle.getUserId() == userInfoOrder.getId()) {
                        Avatars.add(userInfoOrder.getAvatar());
                        nicknames.add(userInfoOrder.getNickname());
                    }
                }
            }
            map.put("avatars", Avatars);
            map.put("nicknames", nicknames);
            map.put("ArtSubject", ArtSubject);
            //总记录数
             total= pageParam.getTotal();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("total", total).data("rows", articleList).data("map", map);
    }


    //添加文章
    @RequestMapping("addArticle")
    public R addarticle(@RequestBody EduArticle eduArticle, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        eduArticle.setUserId(id);
        boolean save = articleService.save(eduArticle);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//根据id查询文章

    @RequestMapping("getarticleByid/{aid}")
    public R getarticleByid(@PathVariable String aid) {
        EduArticle articleServiceById = articleService.getById(aid);
        List uids=new ArrayList();
        uids.add(articleServiceById.getUserId());
        List<UcenterMemberOrder> userInfoOrders = ucenterClient.getUserInfoOrders(uids);
        HashMap<String, List> map = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        for (UcenterMemberOrder userInfoOrder : userInfoOrders) {
            list.add(userInfoOrder.getNickname());
            list.add(userInfoOrder.getAvatar());
        }
        list.add(articleServiceById);
        map.put("item", list);
        return R.ok().data("item", map);
    }


    @GetMapping("getuserinfo")
    public R getuserinfo(HttpServletRequest request) {
        List uids=new ArrayList();
        uids.add(JwtUtils.getMemberIdByJwtToken(request));
        List<UcenterMemberOrder> userInfoOrders = ucenterClient.getUserInfoOrders(uids);
        return R.ok().data("userinfo", userInfoOrders.get(0));
    }
}
